package com.mesnac.comm.page;

public class PageUtil {

	public static Page createPage(Page page,long totalRecords){
		return createPage(page.getEveryPage(), page.getCurrentPage(),totalRecords);
	}
	/**(
	 * 
	* @Title: createPage
	* @Description: 创建一个分页
	* @param @param everyPage
	* @param @param currentPage
	* @param @param totalRecords
	* @param @return    设定文件
	* @return Page    返回类型
	* @throws
	 */
	public static Page createPage(int everyPage,int currentPage,long totalRecords){
		everyPage = getEveryPage(everyPage);
		currentPage = getCurrentPage(currentPage);
		int beginIndex = getBeginIndex(everyPage, currentPage);
		long totalPage = getTotalPage(everyPage, totalRecords);
		boolean hasNextPage = hasNextPage(currentPage, totalPage);
		boolean hasPrePage = hasPrePage(currentPage);
		
		return new Page(hasPrePage,hasNextPage,everyPage,totalPage,currentPage,beginIndex,totalRecords);
	}
	private static int getEveryPage(int everyPage){
		return everyPage == 0? 10:everyPage;
	}
	private static int getCurrentPage(int currentPage){
		return currentPage == 0? 1:currentPage;
	}
	private static int getBeginIndex(int everyPage,int currentPage){
		return (currentPage-1)*everyPage;
	}
	private static long getTotalPage(int everyPage,long totalRecords){
		long totalPage = 0;
		if(totalRecords%everyPage == 0)
			totalPage = totalRecords/everyPage;
		else {
			totalPage = totalRecords/everyPage +1;
		}
		return totalPage;
	}
	private static boolean hasPrePage(int currentPage){
		return currentPage == 1? true:false;
	}
	private static boolean hasNextPage(int currentPage,long totalPage){
		return currentPage == totalPage||totalPage == 0?false:true;
	}
}
